package com.whmnx.my.ui.viewmodel;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.whmnx.base.bean.OrderDetailBean;
import com.whmnx.base.constants.Constants;
import com.whmnx.base.constants.EventConstants;
import com.whmnx.base.http.CallBack;
import com.whmnx.base.http.HttpClient;
import com.whmnx.my.databinding.ActivityXiangBinding;
import com.whmnx.my.ui.activity.AppraiseXiangActivity;
import com.whmnx.my.ui.adapter.AppreaiseListAdapter;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import me.goldze.mvvmhabit.bus.RxBus;
import me.goldze.mvvmhabit.bus.RxSubscriptions;

/**
 * author : huang
 * date   : 2020/11/7 0007 14:50
 * version: 1.0
 */
public class AppreaiseModel2 extends ToolbarViewModel{

    public AppraiseXiangActivity mActivity;
    public ActivityXiangBinding mBinding;
    public  String orderId;
    private AppreaiseListAdapter pingJiaListAdapter;

    private Disposable subscribe;

    public AppreaiseModel2(@NonNull Application application) {
        super(application);
        setTitleText("发表评价");
    }


    public void initToolbar(AppraiseXiangActivity appraiseXiangActivity, ActivityXiangBinding binding, String orderId) {
        this.mActivity = appraiseXiangActivity;
        this.orderId = orderId;
        this.mBinding = binding;

        LinearLayoutManager layoutManager = new LinearLayoutManager(mActivity);
        //配置布局，默认为vertical（垂直布局），下边这句将布局改为水平布局
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mBinding.rvList.setLayoutManager(layoutManager);
        mBinding.rvList.setItemAnimator(new DefaultItemAnimator());

        pingJiaListAdapter = new AppreaiseListAdapter(mActivity);
        mBinding.rvList.setAdapter(pingJiaListAdapter);
        getOrderdetail();
        initView();

    }

    private void getOrderdetail() {
        Map<String, Object> params = new HashMap<>();
        params.put("OrderId",orderId);
        HttpClient.get(mActivity, Constants.ORDERDETAIL, params,new CallBack<OrderDetailBean>() {
            @Override
            public void onSuccess(OrderDetailBean result) {
                pingJiaListAdapter.replaceAll(result.getProductList());
            }
        });
    }


    private void initView() {
        subscribe = RxBus.getDefault().toObservable(String.class)
                .subscribe(new Consumer<String>() {
                    @Override
                    public void accept(String object){
                        if (object.equals(EventConstants.REQUEST)){  //申请退货退款
                             finish();
                        }
                    }
                });
        RxSubscriptions.add(subscribe);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        RxSubscriptions.remove(subscribe);
    }
}
